<?php

namespace app\admin\model\litestore;

use think\Model;


class OrderRefund extends Model
{


    // 表名
    protected $name = 'litestore_order_refund';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'refund_type_text',
        'refund_status_text',
        'refund_time_text',
        'is_del_text',
        'del_time_text'
    ];

    const REFUND_STATUS_APPLY = 0;//申请退款

    const REFUND_STATUS_CANCEL = 1;//取消申请

    const REFUND_STATUS_REFUSE = 2;//拒绝退款

    const REFUND_STATUS_WAIT_BUYER = 3;//等待买家发货

    const REFUND_STATUS_WAIT_SELLER = 4;//等待卖家收货

    const REFUND_STATUS_COMPLETE = 5;//已完成

    const REFUND_TYPE_REFUND = 1;//仅退款

    const REFUND_TYPE_REFUND_RETURN = 2;//退货退款

    public function user()
    {
        return $this->belongsTo('app\\admin\\model\\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goods()
    {
        return $this->belongsTo('Litestoreordergoods', 'order_goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function orderInfo()
    {
        return $this->belongsTo('Litestoreorder', 'order_id', 'id', [], 'LEFT');
    }

    public function getRefundTypeList()
    {
        return ['1' => __('Refund_type 1'), '2' => __('Refund_type 2')];
    }

    public function getRefundStatusList()
    {
        return ['0' => __('Refund_status 0'), '1' => __('Refund_status 1'), '2' => __('Refund_status 2'), '3' => __('Refund_status 3'), '4' => __('Refund_status 4'), '5' => __('Refund_status 5')];
    }

    public function getIsDelList()
    {
        return ['0' => __('Is_del 0'), '1' => __('Is_del 1')];
    }


    public function getRefundTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_type']) ? $data['refund_type'] : '');
        $list = $this->getRefundTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefundStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_status']) ? $data['refund_status'] : '');
        $list = $this->getRefundStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefundTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_time']) ? $data['refund_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsDelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_del']) ? $data['is_del'] : '');
        $list = $this->getIsDelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDelTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['del_time']) ? $data['del_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getRefundMoneyAttr($value)
    {
        return $value ? (float)$value : 0;
    }
}
